﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AutoMapper;

namespace UserGroupData
{
    public class PresentationRepository : IPresentationRepository
    {
        public PresentationDTO GetPresentation(int id)
        {
            using (var ctx = new UserGroupEntities())
            {
                var meeting = ctx.Presentations.Where(d => d.Id == id);
                var entity = meeting.FirstOrDefault();
                Mapper.CreateMap<Presentation, PresentationDTO>();
                var dto = Mapper.Map<Presentation, PresentationDTO>(entity);
                return dto;
            }
        }

        public List<PresentationDTO> GetAllPresentations()
        {
            using (var ctx = new UserGroupEntities())
            {
                var entities = ctx.Presentations.ToList();
                Mapper.CreateMap<Presentation, PresentationDTO>();
                var dto = Mapper.Map<List<Presentation>, List<PresentationDTO>>(entities);
                return dto;
            }
        }

        public int AddNewPresentation(PresentationDTO dto)
        {
            using (var ctx = new UserGroupEntities())
            {
                Mapper.CreateMap<PresentationDTO, Presentation>();
                var entity = Mapper.Map<PresentationDTO, Presentation>(dto);
                ctx.AddToPresentations(entity);
                ctx.SaveChanges();
                return entity.Id;
            }
        }

        public void UpdatePresentation(PresentationDTO dto)
        {
            using (var ctx = new UserGroupEntities())
            {
                Mapper.CreateMap<PresentationDTO, Presentation>();
                var entity = Mapper.Map<PresentationDTO, Presentation>(dto);
                ctx.ApplyCurrentValues("Presentations", entity);
                ctx.SaveChanges();
            }
        }

        public void DeletePresentationDTO(int id)
        {
            using (var ctx = new UserGroupEntities())
            {
                Presentation dbDept = ctx.Presentations.First(d => d.Id == id);
                ctx.DeleteObject(dbDept);
                ctx.SaveChanges();
            }
        }
    }
}
